package com.cn.service.sys.impl;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cn.base.BaseServiceImpl;
import com.cn.dao.sys.SysRoleDao;
import com.cn.sys.entity.SysRoleEntity;
import com.cn.sys.service.SysRoleService;
import com.cn.utils.StringUtil;
import com.github.pagehelper.PageInfo;

/**
 * 角色信息
 * @ClassName: SysRoleServiceImpl
 * @Description: TODO
 * @author Administrator
 * @date 2017年6月8日
 */
//@Service(version="1.0.0", delay = -1)
@Service
@Transactional(readOnly = true)
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {
	@Autowired
	private SysRoleDao sysRoleDao;

	@Override
	public SysRoleEntity queryObject(Long roleId) {
		return sysRoleDao.queryObject(roleId);
	}
	
	public SysRoleEntity get(Map<String, Object> params) {
		return super.get(params);
	}
	
    public  PageInfo<SysRoleEntity> findPage(Map<String, Object> params) {
		return super.findPage(params);
		
	}
	
	@Override
	public List<SysRoleEntity> findList(Map<String, Object> map) {
		return super.findList(map);
	}

	@Transactional(readOnly = false)
	public int save(SysRoleEntity role) throws Exception {
		role.setRoleId(StringUtil.getUUid());
		role.setCreateTime(new Date());
		super.save(role);
		
		//检查权限是否越权
		checkPrems(role);
		
		//保存角色与菜单关系
//		sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
		return 0;
	}

	@Transactional(readOnly = false)
	public int update(SysRoleEntity role) {
		super.update(role);
		
		//检查权限是否越权
		checkPrems(role);
		
		//更新角色与菜单关系
//		sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
		return 0;
	}

	@Override
//	@Transactional(readOnly = false)
	public int deleteBatch(Long[] roleIds) {
		return sysRoleDao.deleteBatch(roleIds);
	}
	
	@Override
	public List<Long> queryRoleIdList(Long createUserId) {
		return sysRoleDao.queryRoleIdList(createUserId);
	}

	/**
	 * 检查权限是否越权
	 */
	private void checkPrems(SysRoleEntity role){
		//如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
//		if(role.getCreateUserId() == Constant.SUPER_ADMIN){
//			return ;
//		}
//		
		//查询用户所拥有的菜单列表
//		List<Long> menuIdList = sysUserService.queryAllMenuId(role.getCreateUserId());
//		
//		//判断是否越权
//		if(!menuIdList.containsAll(role.getMenuIdList())){
//			throw new RRException("新增角色的权限，已超出你的权限范围");
//		}
	}


	
}
